// author: killer
// blog: blog.csdn.net/sabcdefg
// netpt.net
// 武林秘籍 ^^^ 目录 ^^^
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mymultilanguage.h"
#include <QDebug>


extern MyMultiLanguage g_MultiLanguage;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //在帮助之前,动态插入菜单language
    QMenu *languageMenu = new QMenu(tr("language"), menuBar());
    menuBar()->insertMenu(ui->menuHelp->menuAction(), languageMenu);

    //action_zh_CN = new QAction(QIcon(":/res/language/zh_CN.png"), "简体中文", this);
    //action_en_US = new QAction(QIcon(":/res/language/en_US.png"), "英文", this);

    //菜单项"简体中文",注意没有用tr包围
    QAction* action_zh_CN = new QAction("简体中文", this);
    action_zh_CN->setData("zh_CN");
    action_zh_CN->setCheckable(true);

    //菜单项"English",注意没有用tr包围
    QAction* action_en_US = new QAction("English", this);
    action_en_US->setData("en_US");
    action_en_US->setCheckable(true);

    //根据当前的语言设置选中菜单
    if (0 == g_MultiLanguage.GetCurLanguage().compare("zh_CN"))
    {
        action_zh_CN->setChecked(true);
    }
    else if (0 == g_MultiLanguage.GetCurLanguage().compare("en_US"))
    {
        action_en_US->setChecked(true);
    }

    //将简体中文和英文加入到language菜单中去,并设置好处理函数OnLanguageChanged
    QActionGroup* languageGroup = new QActionGroup(this);
    languageMenu->addAction(languageGroup->addAction(action_zh_CN));
    languageMenu->addAction(languageGroup->addAction(action_en_US));
    connect(languageGroup, SIGNAL(triggered(QAction *)), this, SLOT(OnLanguageChanged(QAction *)));

    m_nValue = 100;
    UpdateDynamicControls();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::OnLanguageChanged(QAction* action)
{
    if (NULL != action) {
        // load the language dependant on the action content
        //loadLanguage(action->data().toString());
        //setWindowIcon(action->icon());
        qDebug() << action->data().toString() << "\n";
        g_MultiLanguage.Switch(action->data().toString());
    }
}


void MainWindow::changeEvent(QEvent * event)
{
    if (NULL != event) {
        switch (event->type()) {
            // this event is send if a translator is loaded
        case QEvent::LanguageChange:
            qDebug() << "case QEvent::LanguageChange" << endl;
            ui->retranslateUi(this);
            UpdateDynamicControls();
            break;
        default:
            break;
        }
    }
    QMainWindow::changeEvent(event);

}


void MainWindow::UpdateDynamicControls()
{
    QString strText = QString(tr("Dynamic Text %1")).arg(m_nValue);
    ui->lineEditDynamic->setText(strText);
}



